package com.olap.starter.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.olap.starter.core.base.BaseService;
import com.olap.starter.core.response.Result;
import com.olap.starter.mapper.TreeConfigMapper;
import com.olap.starter.pojo.TableConfig;
import com.olap.starter.pojo.TableConfigColumn;
import com.olap.starter.pojo.TableOperationBut;
import com.olap.starter.pojo.TreeConfig;
import com.olap.starter.service.TableConfigColumnServices;
import com.olap.starter.service.TableOperationButServices;
import com.olap.starter.util.IdUtil;
import com.olap.starter.util.ObjectUtils;
import com.olap.starter.util.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 树形配置 服务实现类
 * </p>
 *
 * @author WBG
 * @since 2024-05-15
 */
@Service
@DS("online")
public class TreeConfigServices extends BaseService<TreeConfigMapper, TreeConfig> {
    @Autowired
    private TableOperationButServices tableOperationButServices;
    @Autowired
    private FormMainServices formMainServices;

    public TreeConfig saveJson(Map<String, Object> params) {
        TreeConfig treeConfig = ObjectUtils.mapToObject((Map) params.get("treeConfig"), TreeConfig.class);
        treeConfig.setCreateTime(null);
        treeConfig.setUpdateTime(null);
        if (treeConfig.getUuid() == null) {
            treeConfig.setUuid(new IdUtil().nextId());
            treeConfig.setVersion(1);
        } else {
            TreeConfig one = getOne(new LambdaQueryWrapper<TreeConfig>().eq(TreeConfig::getUuid, treeConfig.getUuid())
                    .orderByDesc(TreeConfig::getId).select(TreeConfig::getVersion), false);
            update(new LambdaUpdateWrapper<TreeConfig>().set(TreeConfig::getDeleted, 1).eq(TreeConfig::getUuid, treeConfig.getUuid()));
            treeConfig.setVersion(one.getVersion() + 1);
        }
        treeConfig.setId(null);
        save(treeConfig);
        List<Map> toolButOptions = (List) params.get("toolButOptions");
        List<TableOperationBut> tableToolButs = ObjectUtils.listMapToListObject(toolButOptions, TableOperationBut.class);
        tableOperationButServices.saveList(tableToolButs, treeConfig);

        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("formMainList"))) {
            TableConfig tableConfig = new TableConfig();
            tableConfig.setTheme("t1");
            tableConfig.setId(treeConfig.getId());
            tableConfig.setUuid(treeConfig.getUuid());
            formMainServices.saveList(ObjectUtils.listMapToListObject((List) params.get("formMainList"), Map.class), tableConfig);
        }
        return treeConfig;
    }

    public Map<String, Object> getJson(Long uuid) {
        Map<String, Object> data = new HashMap<>();
        TreeConfig treeConfig = getOne(new LambdaQueryWrapper<TreeConfig>().eq(TreeConfig::getUuid, uuid).eq(TreeConfig::getDeleted, 0), false);
        if (treeConfig == null) {
            throw new RuntimeException("uuid异常：" + uuid);
        }
        data.put("treeConfig", treeConfig);
        List<TableOperationBut> list = tableOperationButServices.list(new LambdaQueryWrapper<TableOperationBut>()
                .eq(TableOperationBut::getParentId, treeConfig.getId()).eq(TableOperationBut::getType, 1));
        data.put("toolButOptions", list);
        TableConfig tableConfig = new TableConfig();
        tableConfig.setId(treeConfig.getId());
        tableConfig.setUuid(uuid);
        data.put("formMainList", formMainServices.getList(tableConfig));
        return data;
    }

    @Transactional
    public void saveJSON(MultipartFile[] files) {
        try {
            for (MultipartFile file : files) {
                Map<String, StringBuilder> uuids = ZipUtils.readString(file);
                if (uuids == null || uuids.size() == 0) {
                    throw new RuntimeException("上传异常文件读取不到数据！");
                }
                Collection<StringBuilder> values = uuids.values();
                for (StringBuilder value : values) {
                    JSONObject jsonObject = JSONObject.parseObject(value.toString());
                    saveJson(jsonObject);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
